package com.moxiao.yfjh.module.activity.controller.admin.productType;

import com.moxiao.yfjh.module.activity.common.GenActiveNoCommonUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;

import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;

import com.moxiao.yfjh.module.activity.controller.admin.productType.vo.*;
import com.moxiao.yfjh.module.activity.dal.dataobject.productType.ProductTypeDO;
import com.moxiao.yfjh.module.activity.convert.productType.ProductTypeConvert;
import com.moxiao.yfjh.module.activity.service.productType.ProductTypeService;

@Tag(name = "管理后台 - 相亲商品类型")
@RestController
@RequestMapping("/activity/product-type")
@Validated
public class ProductTypeController {

    @Resource
    private ProductTypeService productTypeService;

    @Resource
    private GenActiveNoCommonUtil genActiveNoCommonUtil;

    @PostMapping("/create")
    @Operation(summary = "创建相亲商品类型")
    @PreAuthorize("@ss.hasPermission('activity:product-type:create')")
    public CommonResult<Long> createProductType(@Valid @RequestBody ProductTypeCreateReqVO createReqVO) {
        genActiveNoCommonUtil.addProductTypeNo(createReqVO);
        return success(productTypeService.createProductType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲商品类型")
    @PreAuthorize("@ss.hasPermission('activity:product-type:update')")
    public CommonResult<Boolean> updateProductType(@Valid @RequestBody ProductTypeUpdateReqVO updateReqVO) {
        productTypeService.updateProductType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲商品类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('activity:product-type:delete')")
    public CommonResult<Boolean> deleteProductType(@RequestParam("id") Long id) {
        productTypeService.deleteProductType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲商品类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('activity:product-type:query')")
    public CommonResult<ProductTypeRespVO> getProductType(@RequestParam("id") Long id) {
        ProductTypeDO productType = productTypeService.getProductType(id);
        return success(ProductTypeConvert.INSTANCE.convert(productType));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲商品类型列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('activity:product-type:query')")
    public CommonResult<List<ProductTypeRespVO>> getProductTypeList(@RequestParam("ids") Collection<Long> ids) {
        List<ProductTypeDO> list = productTypeService.getProductTypeList(ids);
        return success(ProductTypeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲商品类型分页")
    @PreAuthorize("@ss.hasPermission('activity:product-type:query')")
    public CommonResult<PageResult<ProductTypeRespVO>> getProductTypePage(@Valid ProductTypePageReqVO pageVO) {
        PageResult<ProductTypeDO> pageResult = productTypeService.getProductTypePage(pageVO);
        return success(ProductTypeConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲商品类型 Excel")
    @PreAuthorize("@ss.hasPermission('activity:product-type:export')")
    @OperateLog(type = EXPORT)
    public void exportProductTypeExcel(@Valid ProductTypeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ProductTypeDO> list = productTypeService.getProductTypeList(exportReqVO);
        // 导出 Excel
        List<ProductTypeExcelVO> datas = ProductTypeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲商品类型.xls", "数据", ProductTypeExcelVO.class, datas);
    }

}
